package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsShippingAdd;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.UmsShippingAddService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * UmsShippingAdd的路由接口服务
 *
 * @author
 */
@RestController
@Api(tags = "UmsShippingAddController", description = "会员收货地址管理")
@RequestMapping("/addr")
public class UmsShippingAddController {

    /**
     * UmsShippingAddService服务
     */
    @Autowired
    private UmsShippingAddService umsShippingAddService;

    @ApiOperation("查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult list(Principal principal) {
        try {
            if (principal == null) {
                return CommonResult.unauthorized(null);
            }
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            UmsMember umsMember = memberDetails.getUmsMember();
            UmsShippingAdd build = UmsShippingAdd.builder().ueseId(umsMember.getId().intValue()).build();
            List<UmsShippingAdd> commons = umsShippingAddService.find(build);
            return CommonResult.success(commons);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("添加")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(Principal principal, @Valid @RequestBody UmsShippingAdd request) {
        try {
            if (principal == null) {
                return CommonResult.unauthorized(null);
            }
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            UmsMember umsMember = memberDetails.getUmsMember();
            request.setUeseId(umsMember.getId().intValue());
            umsShippingAddService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("查询详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsShippingAdd> getById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            UmsShippingAdd one = umsShippingAddService.findOne(id);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            umsShippingAddService.deleteById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("更新信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@ApiParam(value = "请求报文") @Valid @RequestBody UmsShippingAdd request) {
        try {
            umsShippingAddService.updateNotNullById(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

}
